//
//  ViewController.m
//  CGPDFTest
//
//  Created by Emo, on 2024/5/16.
//

#import "ViewController.h"
#import "TZImagePickerController.h"
#import "PDFTools.h"
#import "PreviewPDFViewController.h"
#import "ImageGridView.h"

#define SCREEN_WIDTH    [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT   [UIScreen mainScreen].bounds.size.height


@interface ViewController () <TZImagePickerControllerDelegate>

@property (nonatomic, strong) ImageGridView * gridView;
@property (weak, nonatomic) IBOutlet UIView *gridBgView;
@property (weak, nonatomic) IBOutlet UITextField *pwdTF;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.gridBgView addSubview:self.gridView];
    
}

- (IBAction)chooseImage:(UIButton *)sender {
    TZImagePickerController *picker = [[TZImagePickerController alloc] initWithMaxImagesCount:9 delegate:self];
    picker.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:picker animated:YES completion:nil];
}

- (IBAction)createPDFAndPreview:(UIButton *)sender {
    if (self.gridView.dataSource > 0) {
        NSString *filePath = [PDFTools localFilePath:@"imageToPdf.pdf"];
        [PDFTools creatPDFWithImagesDataSource:self.gridView.dataSource pdfSize:CGSizeZero pdfPWD:(self.pwdTF.text)?:@"" filePath:filePath];
        
        PreviewPDFViewController *previewVC = [[PreviewPDFViewController alloc] init];
        previewVC.filePath = filePath;
        [self.navigationController pushViewController:previewVC animated:YES];
    }
}

- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray<UIImage *> *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto {
    
    self.gridView.dataSource = [NSMutableArray arrayWithArray:photos];
    
}

#pragma mark -- Lazy --
- (ImageGridView *)gridView {
    if (!_gridView) {
        _gridView = [[ImageGridView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH)];
    }
    return _gridView;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.pwdTF resignFirstResponder];
}

@end
